/*

*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1000;
int mp[maxn][maxn];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,k;
		scanf("%d %d",&n,&k);
		for(int i = 1;i<=n;i++)
		{
			for(int j = 1;j<=n;j++)
			{
				if(k)
				{
					int m = (i+j-1);
					if(m>n) m = m - n;
					mp[j][m] = 1;
					k--;
				}
				else
				{
					int m = (i+j-1);
					if(m>n) m = m - n;
					mp[j][m] = 0;
				} 
			}
		}
		int max1 = -0x3f3f3f3f;
		int min1 =  0x3f3f3f3f;
		for(int i = 1;i<=n;i++)
		{
			int t = 0;
			for(int j = 1;j<=n;j++)
			{
				t += mp[i][j];
			}
			max1 = max(max1,t);
			min1 = min(min1,t);
		}
		int ans1 = max1-min1;
		int max2 = -0x3f3f3f3f;
		int min2 =  0x3f3f3f3f;
		for(int i = 1;i<=n;i++)
		{
			int t = 0;
			for(int j = 1;j<=n;j++)
			{
				t += mp[j][i];
			}
			max2 = max(max2,t);
			min2 = min(min2,t);
		}
		int ans2 = max2-min2;
		printf("%d\n",ans1*ans1+ans2*ans2);
		for(int i = 1;i<=n;i++)
		{
			for(int j = 1;j<=n;j++)
			{
				printf("%d",mp[i][j]);
			}
			printf("\n");
		}
	}
}
